package com.pym.bs.service.Impl;

import com.pym.bs.dto.UserDTO;
import com.pym.bs.dto.UserDetailsDTO;
import com.pym.bs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        System.out.println("登录用户: " + username);
        UserDTO user = userService.findByUsername(username);
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        Assert.notNull(user, "登陆操作[用户不存在]");
        return new UserDetailsDTO(user, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
